package cn.fjy.android.phone;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.AlertDialog.Builder;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import cn.fjy.android.phone.view.RecordFileManager;
import cn.fjy.android.phone.worker.TalkTimeRemind;
import cn.fjy.android.res.Constant;

public class Main extends Activity {
	private boolean callOnVibratorFlag = true;;

	private int callOnVibratorTime = 100;

	private boolean callOffVibratorFlag = true;;

	private int callOffVibratorTime = 100;

	private boolean talkingRemind = false;;

	private int talkingRemindType = TalkTimeRemind.REMIND_TYPE_AUDIO;

	// private int talkingRemindTime = 100;

	private CheckBox callOnCheckBox;
	
	private CheckBox callOffCheckBox;

	private CheckBox talkTimeRemind;
	
	private CheckBox recordInCheckBox;

	private CheckBox recordOutCheckBox;

	private RadioGroup ttRemindType;
	
	private SeekBar vibratorTime;
	
	private SeekBar callOffVibBar;
	
	private TextView callOffValueView;
	
	private TextView vibratorTimeValue;
	
	private String notifyMsg = "";
	
	private boolean notify;
	
	private Notification notification;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initWidget();
		initValue();
		initService();
		connectVibratorConfig();
		talkTimeRemindConfig();
	}

	/**
	 * 初始化服务
	 */
	 private void initService() {
//		 startService(new Intent("cn.fjy.android.phone.phonestateservice"));
	 }

	 
	 /**
	  * 通知栏
	  * @param msg
	  */
	 private void notification(String msg){
		 Intent mIntent = new Intent(this,Main.class);
		 PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
		 if(!notify){
			 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
			 notification = new Notification(R.drawable.icon, getResources().getText(R.string.app_name),0);
			 notification.setLatestEventInfo(this, getResources().getText(R.string.app_name), msg, mPendingIntent);
			 mNotificationManager.notify(0, notification);
		 }else{
			 notification.setLatestEventInfo(this, getResources().getText(R.string.app_name), msg, mPendingIntent);
		 }
	 }
	 
	 /**
	  * 取消通知栏
	  */
	 private void disNotify(){
		 NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
		 manager.cancel(0);
	 }
	 
	 
	/**
	 * 初始化视图控件
	 */
	private void initWidget() {
		callOnCheckBox = (CheckBox) this.findViewById(R.id.checkBox1);
		
		callOffCheckBox = (CheckBox) this.findViewById(R.id.checkBox3);
//
		talkTimeRemind = (CheckBox) this.findViewById(R.id.checkBox2);
		
		recordInCheckBox = (CheckBox)this.findViewById(R.id.record_in);
		
		recordOutCheckBox = (CheckBox)this.findViewById(R.id.record_out);
//
		ttRemindType = (RadioGroup) this.findViewById(R.id.ttRemindType);

		vibratorTime = (SeekBar)findViewById(R.id.vibrator_time);
		
		vibratorTimeValue = (TextView)findViewById(R.id.vibrator_time_value);
		
		callOffVibBar = (SeekBar)findViewById(R.id.callOffVib);
		
		callOffValueView = (TextView)findViewById(R.id.callOffVib_time);
		
		recordInCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton compoundbutton, boolean flag) {
				if(recordInCheckBox.isChecked()){
					if(notify){
						
					}
				}
			}
		});
		
		vibratorTime.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}
			
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean formUser) {
				if(progress%100>0){
					progress = ((progress/100)+1)*100;
				}
				if(progress<100){
					progress = 100;
				}
				seekBar.setProgress(progress);
				vibratorTimeValue.setText(progress+"");
				callOnVibratorTime = progress;
			}
		});
		
		callOffVibBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}
			
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean formUser) {
				if(progress%100>0){
					progress = ((progress/100)+1)*100;
				}
				if(progress<100){
					progress = 100;
				}
				seekBar.setProgress(progress);
				callOffValueView.setText(progress+"");
				callOffVibratorTime = progress;
			}
		});
	}

	/**
	 * 初始化内部属性
	 */
	private void initValue() {
		
		SharedPreferences pre = getSharedPreferences(Constant.CONFIG_FILE_NAME, MODE_PRIVATE);

		callOnVibratorFlag = pre.getBoolean(Constant.PROP_KEY_CALL_ON_VIBRATOR,
				false);

		callOnVibratorTime = pre.getInt(Constant.PROP_KEY_VIBRATOR_TIME, 100);
		
		callOffVibratorTime = pre.getInt(Constant.PROP_KEY_CALL_OFF_VIBRATOR_TIME, 100);
		
		callOffVibratorFlag = pre.getBoolean(Constant.PROP_KEY_CALL_OFF_VIBRATOR,
				false);

		talkingRemind = pre
				.getBoolean(Constant.PROP_KEY_TALKTIME_REMIND, false);

		talkingRemindType = pre.getInt(Constant.PROP_KEY_TALKTIME_REMIND_TYPE,
				TalkTimeRemind.REMIND_TYPE_AUDIO);
		
		recordInCheckBox.setChecked(pre.getBoolean(Constant.PROP_KEY_RECORD_IN, false));
		
		recordOutCheckBox.setChecked(pre.getBoolean(Constant.PROP_KEY_RECORD_OUT, false));

		if (TalkTimeRemind.REMIND_TYPE_VIBRATOR == talkingRemindType) {
			((RadioButton) findViewById(R.id.radioButtonZD)).setChecked(true);
			((RadioButton) findViewById(R.id.radioButtonSY)).setChecked(false);
		} else {
			((RadioButton) findViewById(R.id.radioButtonSY)).setChecked(true);
			((RadioButton) findViewById(R.id.radioButtonZD)).setChecked(false);
		}
		
		callOnCheckBox.setChecked(callOnVibratorFlag);
		
		callOffCheckBox.setChecked(callOffVibratorFlag);
		
		talkTimeRemind.setChecked(talkingRemind);

		vibratorTime.setProgress(callOnVibratorTime);
		
		callOffVibBar.setProgress(callOffVibratorTime);
	}

	/**
	 * 保存用户配置
	 */
	private void saveConfig() {

		SharedPreferences pre = getSharedPreferences(Constant.CONFIG_FILE_NAME, MODE_PRIVATE);
		Editor mEditor = pre.edit();
		mEditor.putBoolean(Constant.PROP_KEY_CALL_ON_VIBRATOR,
				callOnVibratorFlag);
		mEditor.putInt(Constant.PROP_KEY_VIBRATOR_TIME, callOnVibratorTime);

		mEditor.putBoolean(Constant.PROP_KEY_TALKTIME_REMIND, talkingRemind);
		mEditor.putInt(Constant.PROP_KEY_TALKTIME_REMIND_TYPE,
				talkingRemindType);
		
		mEditor.putBoolean(Constant.PROP_KEY_CALL_OFF_VIBRATOR,callOffVibratorFlag);
		mEditor.putInt(Constant.PROP_KEY_CALL_OFF_VIBRATOR_TIME, callOffVibratorTime);
		
		mEditor.putBoolean(Constant.PROP_KEY_RECORD_IN, recordInCheckBox.isChecked());
		
		mEditor.putBoolean(Constant.PROP_KEY_RECORD_OUT, recordOutCheckBox.isChecked());

		mEditor.commit();
	}

	/**
	 * 监听用户推出
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			getCallOnVibratorTime();
			saveConfig();
		}
		if(!callOffVibratorFlag&&!callOnVibratorFlag&&!talkingRemind){
			stopService(new Intent("cn.fjy.android.phone.phonestateservice"));
		}else{
			startService(new Intent("cn.fjy.android.phone.phonestateservice"));
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 开启关闭接通震动
	 */
	private void connectVibratorConfig() {
		callOnCheckBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							callOnVibratorFlag = true;
						} else {
							callOnVibratorFlag = false;
						}
					}
				});
		callOffCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				if (isChecked) {
					callOffVibratorFlag = true;
				} else {
					callOffVibratorFlag = false;
				}
			}
		});
	}

	/**
	 * 获取接通震动时间
	 */
	private void getCallOnVibratorTime() {
		String tempStr = vibratorTimeValue.getText().toString();
		try {
			callOnVibratorTime = Integer.valueOf(tempStr);
		} catch (Exception e) {
			callOnVibratorTime = 100;
		}
		if (callOnVibratorTime > 500 || callOnVibratorTime < 100) {
			callOnVibratorTime = 100;
		}
	}

	private void talkTimeRemindConfig() {
		// 是否开启通话时间提示
		talkTimeRemind
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							talkingRemind = true;
						} else {
							talkingRemind = false;
						}
					}
				});

		// 提示方式
		ttRemindType
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (R.id.radioButtonSY == checkedId) {
							talkingRemindType = TalkTimeRemind.REMIND_TYPE_AUDIO;
						} else if (R.id.radioButtonZD == checkedId) {
							talkingRemindType = TalkTimeRemind.REMIND_TYPE_VIBRATOR;
						}
					}
				});
	}
	//底部菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,0,0,"管理录音文件");
		menu.add(1, 1, 1, "关于");
		return super.onCreateOptionsMenu(menu);
	}
	
	//菜单点击事件
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			Intent mIntent = new Intent(this, RecordFileManager.class);
			startActivity(mIntent);
			break;
		case 1:
			String msg="感谢您安装使用本程序!!!\n当前版本:1.1.1\n该版本增加了通话录音功能，但还不是很稳定.\n不同机型以及ROM版本等都会导致录音失效的问题！\n当前该功能仅供测试使用，谢谢大家的支持！\n如有问题和建议请发邮件到:\ntyfjy@126.com\nBy:GrayF";
			Builder mBuilder  = new AlertDialog.Builder(this);
			mBuilder.setTitle("Android通话辅助程序");
				mBuilder.setMessage(msg);
				mBuilder.setPositiveButton("确定", null);
				mBuilder.show();
			break;	
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}